#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "streamdata.h"

STREAMDATA streamdata;

int handler(void *streamdata, char cmd[])
{
    STREAMDATA *p = streamdata;
    int i = 0;
    printf("command:");
    for (i = 0;; i++)
    {
        printf("%c ", cmd[i]);
        if (cmd[i] == 'e')
        {
            printf("\n");
            break;
        }
    }

    return i + 1;
};

int condition1(void *streamdata, char cmd[])
{
    STREAMDATA *p = streamdata;
    if (cmd[0] != 's')
    {
        return -1;
    }
    else
    {
        return 0;
    }
}
int condition2(void *streamdata, char cmd[])
{
    STREAMDATA *p = streamdata;
    for (int i = 0; i < p->size; i++)
    {
        if (cmd[i] == 'e')
        {
            return 0;
        }
    }
    return -1;
}

int main()
{
    streamdatainit(&streamdata, 32, 5);
    streamdataaddhandle(&streamdata, handler);
    streamdataaddcondition(&streamdata, condition1);
    streamdataaddcondition(&streamdata, condition2);
    while (1)
    {
        printf("请输入指令,以's'开头,'e'结尾:\n");
        int size = 256;
        char *buff = calloc(size, 1);
        fgets(buff, size, stdin);
        size = strlen(buff);
        streamdataadddata(&streamdata, buff, size-1);
        streamdatahandle(&streamdata);
        printf("total command:%d\n", streamdata.count);
        printf("ignore bytes:%d\n", streamdata.ignorecount);
    }

    return 0;
}